package com.example.kehoobackend.controller;


import com.example.kehoobackend.common.CommonResponse;
import com.example.kehoobackend.domain.Answer;
import com.example.kehoobackend.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@CrossOrigin
@RestController
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    /**
     * 查询某问题的所有回答
     *
     * @param question_id 问题id
     * @param user_id 用户id
     * @return CommonResponse<List < Answer>>
     */
    @GetMapping("/answer/question_id")
    public CommonResponse<List<Answer>> getAnswerByQuestionId(
            @RequestParam("question_id") Integer question_id,
            @RequestParam("user_id") Integer user_id) {
        return answerService.getAnswerByQuestionId(question_id,user_id);
    }

    /**
     * 查询某用户的所有回答
     *
     * @param user_id 用户id
     * @return CommonResponse<List < Answer>>
     */
    @GetMapping("/answer/user_id")
    public CommonResponse<List<Answer>> getAnswerByUserId(
            @RequestParam("user_id") Integer user_id) {
        return answerService.getAnswerByUserId(user_id);
    }

    /**
     * 查询某用户是否点赞某个回答
     *
     * @param answer_id 回答id
     * @param user_id 用户id
     * @return CommonResponse<Boolean>
     */
    @GetMapping("/answer/like")
    public CommonResponse<Boolean> isLike(
            @RequestParam("answer_id") Integer answer_id,
            @RequestParam("user_id") Integer user_id) {
        return answerService.isLike(answer_id, user_id)
                ? CommonResponse.createForSuccess("已点赞", true)
                : CommonResponse.createForSuccess("未点赞", false);
    }

    /**
     * 查询所有回答
     *
     * @return CommonResponse<List < Answer>>
     */
    @GetMapping("/answer")
    public CommonResponse<List<Answer>> getAllAnswer() {
        return answerService.getAllAnswer();
    }




    /**
     * 新增回答
     *
     * @param answer 回答
     * @return CommonResponse<Answer>
     */
    @PostMapping("/answer")
    public CommonResponse<Answer> addAnswer(
            @RequestBody Answer answer) {
        return CommonResponse.createForSuccess("新增成功",answerService.addAnswer(answer));
    }


    /**
     * 给回答点赞
     *
     * @param answer_id 回答id
     * @param user_id 用户id
     * @return CommonResponse<Boolean>
     */
    @PatchMapping("/answer/like")
    public CommonResponse<Boolean> likeAnswer(
            @RequestParam("answer_id") Integer answer_id,
            @RequestParam("user_id") Integer user_id) {
        return answerService.like(answer_id, user_id)
                ? CommonResponse.createForSuccess("点赞成功", true)
                : CommonResponse.createForSuccess("点赞失败", false);
    }


    /**
     * 删除回答
     *
     * @param answer_id 回答id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/answer")
    public CommonResponse<Boolean> deleteAnswer(
            @RequestParam("answer_id") Integer answer_id) {
        return answerService.deleteAnswer(answer_id)
                ? CommonResponse.createForSuccess("删除回答成功", true)
                : CommonResponse.createForSuccess("删除回答失败", false);
    }

    /**
     * 取消点赞
     * @param answer_id 回答id
     * @param user_id 用户id
     * @return CommonResponse<Boolean>
     */
    @DeleteMapping("/answer/like")
    public CommonResponse<Boolean> cancelLike(
            @RequestParam("answer_id") Integer answer_id,
            @RequestParam("user_id") Integer user_id) {
        return answerService.cancelLike(answer_id, user_id)
                ? CommonResponse.createForSuccess("取消点赞成功", true)
                : CommonResponse.createForSuccess("取消点赞失败", false);
    }
}
